package com.iknowone.servlet;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iknowone.db.DbManager;
import com.iknowone.entity.Params;
import com.iknowone.entity.Poi;
import com.iknowone.entity.Result;
import com.iknowone.entity.Status;

public class LastRecomandationsServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected Result getResult(HttpServletRequest req) {
		DbManager dbManager = new DbManager();
		String strLimit = req.getParameter(Params.LIMIT.getValue());
		String byFriends = req.getParameter(Params.BY_FRIENDS.getValue());
		List<Poi> recPois = null;
		Status status = checkMandatoryParameter(Params.LIMIT, req);
		if (status.getHttpCode() == HttpServletResponse.SC_OK) {
			try {
				Long limit = Long.valueOf(strLimit);
				recPois = dbManager.lastRecomandations(
						Boolean.valueOf(byFriends), limit);
			} catch (NumberFormatException e) {
				status = new Status(HttpServletResponse.SC_BAD_REQUEST,
						"Invalid number - limit.");
			} catch (Exception e) {
				status = new Status(
						HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
						"Internal error.");
			}
		}
		return new Result(recPois, status);
	}
}